package com.zzx.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zzx.model.Role;
import com.zzx.model.User;
import com.zzx.service.IRoleService;
import com.zzx.service.IUserService;
import com.zzx.util.BaseController;
import com.zzx.util.UuidUtils;

@RestController
public class TestController extends BaseController{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3510315267720514941L;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IRoleService roleService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getList() {
		System.out.println("******后台*******");
		
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		
//		List<Map<String,String>> btnList = new ArrayList<Map<String,String>>();
//		
//		System.out.println("****保存完成");
//		Map<String, String> map = new HashMap<String,String>();
//		map.put("type", "1");
//		map.put("title", "添加");
//		map.put("url", "/user/add");		
//		Map<String, String> map1 = new HashMap<String,String>();
//		map1.put("type", "2");
//		map1.put("title", "编辑");
//		map1.put("url", "/user/edit");		
//		Map<String, String> map2 = new HashMap<String,String>();
//		map2.put("type", "3");
//		map2.put("title", "删除");
//		map2.put("url", "/user/delete");
//		btnList.add(map);
//		btnList.add(map1);
//		btnList.add(map2);
//		
//		//list.add(map);
//		rtnMap.put("btnList" , btnList);
//		
//		
//		//LinuxUtils.getSystemMsg();
//		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
//		for(int i=0;i<333;i++) {
//			Map<String,String> m = new HashMap<String,String>();
//			m.put("userName", "小明"+i);
//			m.put("sex", "男"+i);
//			m.put("telNum", "134"+i);
//			map.put("age", String.valueOf(i+17));
//			list.add(m);
//		}
		Map<String, String> params = new HashMap<String,String>();
		String id = getPara("id");
		params.put("id", id);
		List<User> list = userService.findAll(params);
		int count = userService.getCount(params);
		rtnMap.put("datas", list);
		Map<String, Object> page = new HashMap<String,Object>();
		page.put("pageSize", 10);
		page.put("pagerCount", 11);
		page.put("total", list.size());
		rtnMap.put("page" , page);
		System.out.println("****更改后台**111*********");
		return rtnMap;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public Map<String,Object> save(User user) {
		System.out.println("******save 后台*******");
		//User user = new User();
		user.setId(UuidUtils.getCurtimeInt());
		//user.setName("明明");
		//user.setAddress("中国");
		//user.setAge(30);
		//user.setSex(2);
		userService.save(user);
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		rtnMap.put("message", "数据保存成功");
		rtnMap.put("success", 02);
		System.out.println("****更改后台**222*********");
		return rtnMap;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public Map<String,Object> update(User user) {
		System.out.println("******update 后台*******");
		//User user = new User();		
		//user.setName("明明");
		//user.setAddress("中国");
		//user.setAge(30);
		//user.setSex(2);
		userService.update(user);
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		rtnMap.put("message", "数据修改成功");
		rtnMap.put("success", 02);
		System.out.println("****更改后台**333*********");
		return rtnMap;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public Map<String,Object> deleteById() {
		System.out.println("******update 后台*******");
		//User user = new User();
		//user.setId(UuidUtils.getCurtimeInt());
		//user.setName("明明");
		//user.setAddress("中国");
		//user.setAge(30);
		//user.setSex(2);
		String id = getPara("id");
		
		userService.deleteById(Integer.parseInt(id));
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		rtnMap.put("message", "数据删除成功");
		rtnMap.put("success", 02);
		System.out.println("****更改后台**444*********");
		return rtnMap;
	}
	
	@RequestMapping(value = "/getById", method = RequestMethod.GET)
	public Map<String,Object> getById() {
		System.out.println("******update 后台*******");
		//User user = new User();
		//user.setId(UuidUtils.getCurtimeInt());
		//user.setName("明明");
		//user.setAddress("中国");
		//user.setAge(30);
		//user.setSex(2);
		String id = getPara("id");
		
		User user = userService.getById(Integer.parseInt(id));
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		rtnMap.put("message", "数据获取成功");
		rtnMap.put("success", 02);
		rtnMap.put("user", user);
		System.out.println("****更改后台**555*********");
		return rtnMap;
	}
	
	@RequestMapping(value = "/getRoleDtl", method = RequestMethod.GET)
	public Map<String,Object> getRoleDtlById() {
		System.out.println("******update 后台*******");
		
		String id = getPara("id");		
		Role user = roleService.getById(id);
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		rtnMap.put("message", "数据获取成功");
		rtnMap.put("success", 02);
		rtnMap.put("user", user);
		System.out.println("****更改后台**555*********");
		return rtnMap;
	}
	
}
